home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / FAQ.SWG / 0002_Dos Programming FAQ.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  64KB  |  1,340 lines

  1. =====================
  2. section 1. General questions
  3.     101. Why won't my code work?
  4.     102. What is this newsgroup about?
  5.     103. What's the difference from comp.sys.ibm.pc.programmer?
  6.     104. What other newsgroups should I know about?
  7.  
  8. section 2. Compile and link
  9.     201. What the heck is "DGROUP > 64K"?
  10.     202. How do I fix "automatic data segment exceeds 64K" or "stack
  11.          plus data exceed 64K"?
  12.     203. Will Borland C code and Microsoft C code link together?
  13.     204. Why did my program bomb at run time with "floating point
  14.          formats not linked"?
  15.     205. Why did my program bomb with "floating point not loaded"?
  16.     206. How can I change the stack size in Borland's C compilers?
  17.     207. What's the format of an .OBJ file?
  18.     208. What's the format of an .EXE header?
  19.     209. What's the difference between .COM and .EXE formats?
  20.  
  21. section 3. Keyboard
  22.     301. How can I read a character without echoing it to the screen,
  23.          and without waiting for the user to press the Enter key?
  24.     302. How can I find out whether a character has been typed, without
  25.          waiting for one?
  26.     303. How can I disable Ctrl-C/Ctrl-Break and/or Ctrl-Alt-Del?
  27.     304. How can I disable the print screen function?
  28.     305. How can my program turn NumLock (CapsLock, ScrollLock) on/off?
  29.     306. How can I speed up the keyboard's auto-repeat?
  30.     307. What is the SysRq key for?
  31.     308. How can my program tell what kind of keyboard is on the system?
  32.     309. How can I tell if input, output, or stderr has been redirected?
  33.  
  34. section 4. Disks and files
  35.     401. What drive was the PC booted from?
  36.     402. How can I boot from drive b:?
  37.     403. Which real and virtual disk drives are valid?
  38.     404. How can I make my single floppy drive both a: and b:?
  39.     405. Why won't my C program open a file with a path?
  40.     406. How can I redirect printer output to a file?
  41.     407. How can my program open more files than DOS's limit of 20?
  42.     408. How can I read, create, change, or delete the volume label?
  43.     409. How can I get the disk serial number?
  44.     410. What's the format of .OBJ, .EXE., .COM files?
  45.  
  46. section 5. Serial ports (COM ports)
  47.     501. How do I set my machine up to use COM3 and COM4?
  48.     502. How do I find the I/O address of a COM port?
  49.     503. But aren't the COM ports always at I/O addresses 3F8, 2F8, 3E8,
  50.          and 2E8?
  51.     504. How do I configure a COM port and use it to transmit data?
  52. section 6. Other hardware questions and problems
  53.     601. Which 80x86 CPU is running my program?
  54.     602. How can a C program send control codes to my printer?
  55.     603. How can I redirect printer output to a file?
  56.     604. Which video adapter is installed?
  57.     605. How do I switch to 43- or 50-line mode?
  58.     606. How can I find the Microsoft mouse position and button status?
  59.     607. How can I access a specific address in the PC's memory?
  60.     608. How can I read or write my PC's CMOS memory?
  61.     609. How can I access memory beyond 640K?
  62. section 7. Other software questions and problems
  63.     701. How can a program reboot my PC?
  64.     702. How can I time events with finer resolution than the system
  65.          clock's 55 ms (about 18 ticks a second)?
  66.     703. How can I find the error level of the previous program?
  67.     704. How can a program set DOS environment variables?
  68.     705. How can I change the switch character to - from /?
  69.     706. Why does my interrupt function behave strangely?
  70.     707. How can I write a TSR (terminate-stay-resident) utility?
  71.     708. How can I write a device driver?
  72.     709. What can I use to manage versions of software?
  73.     710. What's this "null pointer assignment" after my C program
  74.          executes?
  75.  
  76. section A. Downloading
  77.     A01. What is garbo?  What is wustl?
  78.     A02. What are Simtel and "mirror sites"?  What good are they?
  79.     A03. Where do I find program <mumble>?
  80.     A04. How can I check Simtel or garbo before I post a request for a
  81.          program?
  82.     A05. How do I download and decode a program I found?
  83.     A06. Where is UUDECODE?
  84.     A07. Why do I get errors when extracting from a ZIP file I
  85.          downloaded?
  86.  
  87. section B. Vendors and products
  88.     B01. How can I contact Borland?
  89.     B02. How can I contact Microsoft?
  90.     B03. What's the current version of PKZIP?
  91.     B04. What's in Borland Pascal/Turbo Pascal 7.0?
  92.     B05. What's in Microsoft C/C++ 7.0?
  93. section C. More information
  94.     C01. Are there any good on-line references for PC hardware
  95.          components?
  96.     C02. Are there any good on-line references for PC interrupts?
  97.     C03. What and where is "Ralf Brown's interrupt list"?
  98.     C04. Where can I find lex, yacc, and language grammars?
  99.     C05. What's the best book to learn programming?
  100.     C06. Where are FAQ lists archived?
  101.     C07. Where can I get the latest copy of this FAQ list?
  102.  
  103.  
  104.  
  105. section 1. General questions
  106. ============================
  107.  
  108. Q101. Why won't my code work?
  109.  
  110.     First you need to try to determine whether the problem is in your
  111.     use of the programming language or in your use of MSDOS and your PC
  112.     hardware.  (Your manual should tell you which features are standard
  113.     and which are vendor- or MSDOS- or PC-specific.  You _have_ read
  114.     your manual carefully, haven't you?)
  115.  
  116.     If the feature that seems to be working wrong is something related
  117.     to your PC hardware or to the internals of MS-DOS, this group is the
  118.     right place to ask.  (Please check this list first, to make sure
  119.     your question isn't already answered.)
  120.  
  121.     On the other hand, if your problem is with the programming language,
  122.     the comp.lang hierarchy (including comp.lang.pascal and comp.lang c)
  123.     is probably a better resource.  Please read the other group's FAQ
  124.     list thoroughly before posting.  (These exist in comp.lang.c,
  125.     comp.lang.c++, comp.lang.modula3, comp.lang.lisp, comp.lang.perl;
  126.     they may exist in other groups as well.)  It's almost never a good
  127.     idea to crosspost between this group and a language group.
  128.  
  129.     Before posting in either place, try to make your program as small as
  130.     possible while still exhibiting the bad behavior.  Sometimes this
  131.     alone is enough to show you where the trouble is.  Also edit your
  132.     description of the problem to be as short as possible.  This makes
  133.     it look more like you tried to solve the problem on your own, and
  134.     makes people more inclined to try to help you.
  135.  
  136.     When you do post a question, it's good manners to say "email please;
  137.     I'll post a summary."  Then everybody else in the group doesn't have
  138.     to read ten virtually identical responses.  Of course, then you have
  139.     to follow through.  A summary is not simply pasting together all the
  140.     email you received.  Instead, write your own (brief) description of
  141.     the solution:  this is the best way to make sure you really
  142.     understand it.  Definitely don't repost people's cute signatures.
  143.  
  144. Q102. What is this newsgroup about?
  145.  
  146.     comp.os.msdos.programmer (comp.sys.ibm.pc.programmer until September
  147.     1990) concerns programming for MS-DOS systems.  The article "USENET
  148.     Readership report for Nov 92" in news.lists shows 42,000 readers of
  149.     this newsgroup worldwide.  Traffic was 1090.7 Kbytes (exclusive of
  150.     crossposts), comprised in 611 articles.
  151.  
  152.     Much of our traffic is about language products (chiefly from Borland
  153.     and Microsoft).  More programming topics focus on C than on any one
  154.     other language.
  155.  
  156.     Since most MS-DOS systems run on hardware that is roughly compatible
  157.     with the IBM PC, on Intel 8088, 80188, or 80x86 chips, we tend to
  158.     get a lot of questions and answers about programming other parts of
  159.     the hardware.
  160.  
  161. Q103. What's the difference from comp.sys.ibm.pc.programmer?
  162.  
  163.     c.s.i.p.programmer is the old name of comp.os.msdos.programmer, and
  164.     has been obsolete since September 1990.  However, many systems have
  165.     not removed the old group, or have removed it but aliased it to the
  166.     new name.  This means that some people still think they're posting
  167.     to c.s.i.p.programmer even though they're actually posting to
  168.     c.o.m.programmer.
  169.  
  170.     You can easily verify the non-existence of c.s.i.p.programmer by
  171.     reference to the "List of Active Newsgroups" posted to news.groups.
  172.     It's available as /pub/usenet/news.answers/active-newsgroups/part1
  173.     from the archives (see "Where are FAQ lists archived?" in section C,
  174.     "More information").
  175.  
  176. Q104. What other newsgroups should I know about?
  177.  
  178.     Your best bet is to read the periodic information postings in the
  179.     comp.binaries.ibm.pc newsgroup.  Specially helpful articles:
  180.         Using the comp.binaries.ibm.pc.d groups
  181.         Beginner's guide to binaries
  182.         Starter kit
  183.         About archives and archivers
  184.     Please wait for these articles to come around; don't post a request.
  185.  
  186.     Also check out news.announce.newusers, even if you're not a new
  187.     user.  You may be surprised how much useful information is in the
  188.     monthly postings there.  Lots of old-timers also get useful stuff
  189.     from news.newusers.questions, especially the periodic postings.
  190.  
  191.     Remember that it's good manners to subscribe to any newsgroup and
  192.     read it for a while before you post a question.  When you post, it's
  193.     also good manners to ask for replies to be emailed and then to post
  194.     a summary, which you've edited down to the absolute minimum size.
  195.  
  196.     You may also be interested in the following newsgroups.  Caution:
  197.     Some of them have specialized charters; you'll probably get (and
  198.     deserve) some flames if you post to an inappropriate group.
  199.  
  200.     - misc.forsale.computers and misc.forsale.computers.pc-clone are
  201.       where you post notices of equipment, software, or computer books
  202.       that you want to sell.  Please don't post or crosspost those
  203.       notices to comp.os.msdos.programmer.
  204.  
  205.     - comp.os.ms-windows.programmer.tools and ...misc (formerly part of
  206.       comp.windows.ms.programmer):  Similar to this group, but focus
  207.       on programming for the MS-Windows platform.
  208.  
  209.     - comp.sys.ibm.pc.hardware is for more hardware-oriented discussions
  210.       of the machines that run DOS.
  211.  
  212.     - comp.binaries.ibm.pc.wanted: AFTER you have looked in the other
  213.       groups, this is the place to post a request for a particular
  214.       binary program.
  215.  
  216.     - comp.binaries.msdos.announce (moderated) explains how to use the
  217.       archive sites, especially garbo and Simtel, and lists files
  218.       uploaded to them.  Discussions belong in comp.binaries.msdos.d,
  219.       which replaced comp.binaries.ibm.pc.archives.
  220.  
  221.     - comp.binaries.ibm.pc.d is for discussions about programs posted in
  222.       comp.binaries.ibm.pc, and only those programs.  This is a good
  223.       place to report bugs in the programs, but not to ask where to find
  224.       them (see cbip.wanted, above).  cbip.d is NOT supposed to be a
  225.       general PC discussion group.
  226.  
  227.     - comp.sources.misc: a moderated group for source code for many
  228.       computer systems.  It tends to get lots of Unix stuff, but you may
  229.       also pick up some DOS-compatible code here.
  230.  
  231.     - alt.sources: an unmoderated group for source code.  Guidelines are
  232.       posted periodically.
  233.  
  234.     - Turbo Vision is a mailing list, not a newsgroup; send email to
  235.       listserv@vtvm1.cc.vt.edu if you want to subscribe.
  236.  
  237.  
  238. section 2. Compile and link
  239. ===========================
  240.  
  241. Q201. What the heck is "DGROUP > 64K"?
  242.  
  243.     DGROUP is a link-time group of data segments, and the compiler
  244.     typically generates code that expects DS to be pointing to DGROUP.
  245.     (Exception: Borland's huge model has no DGROUP.)
  246.  
  247.     Here's what goes into DGROUP:
  248.  
  249.     - tiny model (all pointers near):  DGROUP holds the entire program.
  250.  
  251.     - small and medium models (data pointers near):  DGROUP holds all
  252.       globals and static variables including string literals, plus the
  253.       stack and the heap.
  254.  
  255.     - large, compact, and huge models in Microsoft (data pointers far):
  256.       DGROUP holds only initialized globals and static variables
  257.       including string literals, plus the stack and the near heap.
  258.  
  259.     - large and compact models in Borland (data pointers far): DGROUP
  260.       holds initialized and uninitialized globals and static variables
  261.       including string literals, but not the stack or heap.
  262.  
  263.     - huge model in Borland (data pointers far): there is no DGROUP, so
  264.       the 64K limit doesn't apply.
  265.  
  266.     In all of the above, which is to say all six models in Microsoft C
  267.     and all but huge in Borland C, DGROUP is limited to 64K including
  268.     string literals (which are treated as static data).  This limitation
  269.     is due to the Intel CPU's segmented architecture.
  270.  
  271.     See the next Q for possible remedies.
  272.  
  273.     For more information, see topics like "memory models" and "memory
  274.     management" in the index of your compiler manual.  Also see
  275.     TI738.ASC in PD1:<MSDOS.TURBO-C>BCHELP10.ZIP at Simtel for an
  276.     extended general discussion of memory usage in Borland C programs,
  277.     of which much applies to any C compiler in DOS.
  278.  
  279. Q202. How do I fix "automatic data segment exceeds 64K" or "stack plus
  280.       data exceed 64K"?
  281.  
  282.     These messages are a variation of "DGROUP > 64K".  For causes,
  283.     please see the preceding Q.
  284.  
  285.     If you get this error in tiny model, your program is simply too big
  286.     and you must use a different memory model.  If you get this link
  287.     error in models S, C, M, L, or Microsoft's H, there are some things
  288.     you can do.  (This error can't occur in Borland's huge model.)
  289.  
  290.     If you have one or two big global arrays, simply declare them far.
  291.     The compiler takes this to mean that any references to them will use
  292.     32-bit pointers, so they'll be in separate segments and no longer
  293.     part of DGROUP.
  294.  
  295.     Or you can use the /Gt[number] option with Microsoft or -Ff[=size]
  296.     with Borland C++ 2.0 and up.  This will automatically put variables
  297.     above a certain size into their own segments outside of DGROUP.
  298.  
  299.     Yet another option is to change global arrays to far pointers.  Then
  300.     at the beginning of your program, allocate them from the far heap
  301.     (_fmalloc in Microsoft, farmalloc in Borland).
  302.  
  303.     Finally, you can change to huge model (with Borland compilers, not
  304.     Microsoft).  Borland's H model still uses far pointers by default,
  305.     but "sets aside the [64K] limit" and has no DGROUP group, according
  306.     to the BC++ 2.0 Programmer's Guide.  Microsoft's H model does use
  307.     huge data pointers by default but retains DGROUP and its 64K limit,
  308.     so switching to the H model doesn't buy you anything if you have
  309.     DGROUP problems.
  310.  
  311. Q203. Will Borland C code and Microsoft C code link together?
  312.  
  313.     Typically this question is asked by someone who owns compiler A and
  314.     is trying to write code to link with a third-party library that was
  315.     compiled under compiler B.
  316.  
  317.     The answer to the question is, Not in general.  Here are some of the
  318.     reasons:
  319.  
  320.     - "Helper" functions (undocumented functions for stack checking,
  321.       floating-point arithmetic, and operations on longs) differ between
  322.       the two compilers.
  323.  
  324.     - The compilers may embed instructions in the object code that tell
  325.       the linker to look for their own run-time libraries.
  326.  
  327.     Those problems will generate link-time errors.  Others may not show
  328.     up until run time:
  329.  
  330.     - Borland's compact, large, and huge models don't assume DS=SS, but
  331.       Microsoft's do.  The -Fs option on the Borland compiler, or one of
  332.       the /A options on Microsoft, should take care of this problem --
  333.       once you know that's what's going on.
  334.  
  335.     - Check conventions for ordering and packing structure members, and
  336.       for alignment of various types on byte, word, paragraph, or other
  337.       boundaries.  Again, you can generally adjust your code to match if
  338.       you know what conventions were used in compiling the "foreign"
  339.       libraries.
  340.  
  341.     - Check the obvious and make sure that your code was compiled under
  342.       the same memory model as the code you're trying to link with.
  343.       (That's necessary, but no guarantee.  Microsoft and Borland don't
  344.       use exactly the same conventions for segments and groups,
  345.       particularly in the larger memory models.)
  346.  
  347.     That said, there are some circumstances where you can link hybrids.
  348.     Your best chance of success comes if you avoid longs and floating
  349.     point, use only 16-bit pointers, suppress stack checking, and
  350.     specify all libraries used in the link.
  351.  
  352. Q204. Why did my program bomb at run time with "floating point formats
  353.       not linked"?
  354.  
  355.     First, is that the actual message, or did it say "floating point not
  356.     loaded"?  If it was the latter, see the next Q.
  357.  
  358.     You're probably using a Borland compiler for C or C++ (including
  359.     Turbo C and Turbo C++).  Borland's compilers try to be smart and not
  360.     link in the floating-point (f-p) library unless you need it.  Alas,
  361.     they all get the decision wrong.  One common case is where you don't
  362.     call any f-p functions, but you have %f or other f-p formats in
  363.     scanf/printf calls.  The cure is to call an f-p function, or at
  364.     least force one to be present in the link.
  365.  
  366.     To do that, define this function somewhere in a source file but
  367.     don't call it:
  368.  
  369.         static void forcefloat(float *p)
  370.             { float f = *p; forcefloat(&f); }
  371.  
  372.     It doesn't have to be in the module with the main program, as long
  373.     as it's in a module that will be included in the link.
  374.  
  375.     A new solution for Borland C++ 3.0 was posted, but I don't own the
  376.     product and have not been able to verify it.  Insert these
  377.     statements in your program:
  378.  
  379.         extern unsigned _floatconvert;
  380.         #pragma extref _floatconvert
  381.  
  382. Q205. Why did my program bomb with "floating point not loaded"?
  383.  
  384.     That is Microsoft C's run-time message when the code requires a
  385.     numeric coprocessor but your computer doesn't have one installed.
  386.  
  387.     If the program is yours, relink it using the xLIBCE or xLIBCA
  388.     library (where x is the memory model).
  389.  
  390. Q206. How can I change the stack size in Borland's C compilers?
  391.  
  392.     In Turbo C, Turbo C++, and Borland C++, you may not find "stack
  393.     size" in the index but the global variable _stklen should be there.
  394.     The manual will instruct you to put a statement like
  395.  
  396.         extern unsigned _stklen = 54321U;
  397.  
  398.     in your code, outside of any function.  You must assign the value
  399.     right in the extern statement; it won't work to assign a value at
  400.     run time.  (The "extern" in this context isn't ANSI C and ought not
  401.     to be required, but the above statement is a direct quote from the
  402.     Library Reference manual of Borland C++ 2.0.)  The linker may give
  403.     you a duplicate symbol warning, which you can ignore.
  404.  
  405. Q207. What's the format of an .OBJ file?
  406.  
  407.     Here's what I've been told, though I have verified any of these
  408.     references myself:
  409.  
  410.     - base .OBJ format:  Intel's document number #121748-001, {8086
  411.       Relocatable Object Module Formats}.  (Note however that both
  412.       Microsoft and Borland formats have extended the .OBJ format.)
  413.  
  414.     - Microsoft-specific .OBJ formats:  a "Microsoft OMF Specification"
  415.       (document number ??), as well as a section in the MS-DOS
  416.       encyclopedia.
  417.  
  418.     - A "tutorial on the .OBJ format" comes with the VAL experimental
  419.       linker, which is VAL-LINK.ARC in PD1:<MSDOS.PGMUTL> at Simtel.
  420.  
  421.     If you have specific references, either to fpt-able documents or to
  422.     published works (author, title, order number or ISBN), please email
  423.     them to brown@ncoast.org for inclusion in the next edition of this
  424.     list.
  425.  
  426. Q208. What's the format of an .EXE header?
  427.  
  428.     See pages 349-350 of {PC Magazine}'s June 30, 1992 issue (xi:12) for
  429.     the old and new formats.  For a more detailed layout, look under INT
  430.     21 function 4B in Ralf Brown's interrupt list.  Ralf Brown's list
  431.     includes extensions for Borland's TLINK and Borland debugger info.
  432.  
  433.     Among the books that detail formats of executable files are {DOS
  434.     Programmer's Reference: 2d Edition} by Terry Dettman and Jim Kyle,
  435.     ISBN 0-88022-458-4; and {Microsoft MS-DOS Programmer's Reference},
  436.     ISBN 1-55615-329-5.
  437.  
  438. Q209. What's the difference between .COM and .EXE formats?
  439.  
  440.     To oversimplify:  a .COM file is a direct image of core, and an .EXE
  441.     file will undergo some further relocation when it is run (and so it
  442.     begins with a relocation header).  A .COM file is limited to 64K for
  443.     all segments combined, but an .EXE file can have as many segments as
  444.     your linker will handle and be as large as RAM can take.
  445.  
  446.     The actual file extension doesn't matter.  DOS knows that a file
  447.     being loaded is in .EXE format if its first two bytes are MZ or ZM;
  448.     otherwise it is assumed to be in .COM format.  For instance, I am
  449.     told that DR-DOS 6.0's COMMAND.COM is in .EXE format.
  450.  
  451.  
  452. section 3. Keyboard
  453. ===================
  454.  
  455. Q301. How can I read a character without echoing it to the screen, and
  456.       without waiting for the user to press the Enter key?
  457.  
  458.     The C compilers from Microsoft and Borland offer getch (or getche to
  459.     echo the character); Turbo Pascal has ReadKey.
  460.  
  461.     In other programming languages, load 8 in register AH and execute
  462.     INT 21; AL is returned with the character from standard input
  463.     (possibly redirected).  If you don't want to allow redirection, or
  464.     you want to capture Ctrl-C and other special keys, use INT 16 with
  465.     AH=10; this will return the scan code in AH and ASCII code (if
  466.     possible) in AL, except that AL=E0 with AH nonzero indicates one of
  467.     the grey "extended" keys was pressed.  (If your BIOS doesn't
  468.     support the extended keyboard, use INT 16 function 0 not 10.)
  469.  
  470. Q302. How can I find out whether a character has been typed, without
  471.       waiting for one?
  472.  
  473.     In Turbo Pascal, use KeyPressed.  Both Microsoft C and Turbo C offer
  474.     the kbhit( ) function.  All of these tell you whether a key has been
  475.     pressed.  If no key has been pressed, they return that information
  476.     to your program.  If a keystroke is waiting, they tell your program
  477.     that but leave the key in the input buffer.
  478.  
  479.     You can use the BIOS call, INT 16 function 01 or 11, to check
  480.     whether an actual keystroke is waiting; or the DOS call, INT 21
  481.     function 0B, to check for a keystroke from stdin (subject to
  482.     redirection).  See Ralf Brown's interrupt list.
  483.  
  484. Q303. How can I disable Ctrl-C/Ctrl-Break and/or Ctrl-Alt-Del?
  485.  
  486.     You can download the file PD1:<MSDOS.KEYBOARD>CADEL.ZIP from Simtel.
  487.     It contains a TSR to disable those keys, with source code in ASM.
  488.  
  489.     To disable only Ctrl-Alt-Del (actually, to change the boot keys to
  490.     leftShift-Alt-Del), use DEBOOT.COM.  Along with KEYKILL.COM, which
  491.     lets you disable up to three keys of your choice, it is at Simtel in
  492.     the file PD1:<MSDOS.KEYBOARD>KEYKILL.ARC.
  493.  
  494.     C programmers who simply want to make sure that the user can't
  495.     Ctrl-Break out of their program can use the ANSI-standard signal( )
  496.     function; the Borland compilers also offer ctrlbrk( ) for handling
  497.     Ctrl-Break.  However, if your program uses normal DOS input, the
  498.     characters ^C will appear on the screen when the user presses Ctrl-C
  499.     or Ctrl-Break.  There are many ways to work around that, including:
  500.     use INT 21 function 7, which allows redirection but doesn't display
  501.     the ^C (or echo any other character, for that matter); or use INT 16
  502.     function 0 or 10; or call _bios_keybrd( ) in MSC or bioskey( ) in
  503.     BC++; or hook INT 9 to discard Ctrl-C and Ctrl-Break before the
  504.     regular BIOS keyboard handler sees them; etc., etc.
  505.  
  506.     You should be aware that Ctrl-C and Ctrl-Break are processed quite
  507.     differently internally.  Ctrl-Break, like all keystrokes, is
  508.     processed by the BIOS code at INT 9 as soon as the user presses the
  509.     keys, even if earlier keys are still in the keyboard buffer:  by
  510.     default the handler at INT 1B is called.  Ctrl-C is not special to
  511.     the BIOS, nor is it special to DOS functions 6 and 7; it _is_
  512.     special to DOS functions 1 and 8 when at the head of the keyboard
  513.     buffer.  You will need to make sure BREAK is OFF to prevent DOS
  514.     polling the keyboard for Ctrl-C during non-keyboard operations.
  515.  
  516.     Some good general references are {Advanced MS-DOS} by Ray Duncan,
  517.     ISBN 1-55615-157-8; {8088 Assembler Language Programming:  The IBM
  518.     PC}, ISBN 0-672-22024-5, by Willen & Krantz; and {COMPUTE!'s Mapping
  519.     the IBM PC}, ISBN 0-942386-92-2.
  520.  
  521. Q304. How can I disable the print screen function?
  522.  
  523.     There are really two print screen functions:  1) print current
  524.     screen snapshot, triggered by PrintScreen or Shift-PrtSc or
  525.     Shift-grey*, and 2) turn on continuous screen echo, started and
  526.     stopped by Ctrl-P or Ctrl-PrtSc.
  527.  
  528.     1) Screen snapshot to printer
  529.        --------------------------
  530.  
  531.     The BIOS uses INT 5 for this.  Fortunately, you don't need to mess
  532.     with that interrupt handler.  The standard handler, in BIOSes dated
  533.     December 1982 or later, uses a byte at 0040:0100 (alias 0000:0500)
  534.     to determine whether a print screen is currently in progress.  If it
  535.     is, pressing PrintScreen again is ignored.  So to disable the screen
  536.     snapshot, all you have to do is write a 1 to that byte.  When the
  537.     user presses PrintScreen, the BIOS will think that a print screen is
  538.     already in progress and will ignore the user's keypress.  You can
  539.     re-enable PrintScreen by zeroing the same byte.
  540.  
  541.     Here's some simple code:
  542.  
  543.         void prtsc_allow(int allow) /* 0=disable, nonzero=enable */ {
  544.             unsigned char far* flag = (unsigned char far*)0x00400100UL;
  545.             *flag = (unsigned char)!allow;
  546.         }
  547.  
  548.     2) Continuous echo of screen to printer
  549.        ------------------------------------
  550.  
  551.     If ANSI.SYS is loaded, you can easily disable the continuous echo of
  552.     screen to printer (Ctrl-P or Ctrl-PrtSc).  Just redefine the keys by
  553.     "printing" strings like these to the screen (BASIC print, C printf,
  554.     Pascal Write statements, or ECHO command in batch files):
  555.  
  556.         <27>[0;114;"Ctrl-PrtSc disabled"p
  557.         <27>[16;"^P"p
  558.  
  559.     Change <27> in the above to an Escape character, ASCII 27.
  560.  
  561.     If you haven't installed ANSI.SYS, I can't offer an easy way to
  562.     disable the echo-screen-to-printer function.  Please send any tested
  563.     solutions to brown@ncoast.org and I'll add them to this list.
  564.  
  565.     Actually, you might not need to disable Ctrl-P and Ctrl-PrtSc.  If
  566.     your only concern is not locking up your machine, when you see the
  567.     "Abort, Retry, Ignore, Fail" prompt just press Ctrl-P again and then
  568.     I.  As an alternative, install one of the many print spoolers that
  569.     intercept printer-status queries and always return "Printer ready".
  570.  
  571. Q305. How can my program turn NumLock (CapsLock, ScrollLock) on or off?
  572.  
  573.     You need to twiddle bit 5, 6, or 4 of location 0040:0017.  Here's
  574.     some code:  lck( ) turns on a lock state, and unlck( ) turns it off.
  575.     (The status lights on some keyboards may not reflect the change.  If
  576.     yours is one, call INT 16 function 2, "get shift status", and that
  577.     may update them.  It will certainly do no harm.)
  578.  
  579.         #define NUM_LOCK  (1 << 5)
  580.         #define CAPS_LOCK (1 << 6)
  581.         #define SCRL_LOCK (1 << 4)
  582.         void lck(int shiftype) {
  583.             char far* kbdstatus = (char far*)0x00400017UL;
  584.             *kbdstatus |= (char)shiftype;
  585.         }
  586.         void unlck(int shiftype) {
  587.             char far* kbdstatus = (char far*)0x00400017UL;
  588.             *kbdstatus &= ~(char)shiftype;
  589.         }
  590.  
  591. Q306. How can I speed up the keyboard's auto-repeat?
  592.  
  593.     The keyboard speed has two components: delay (before a key that you
  594.     hold down starts repeating) and typematic rate (the speed once the
  595.     key starts repeating).  Most BIOSes since 1986 let software change
  596.     the delay and typematic rate by calling INT 16 function 3, "set
  597.     typematic rate and delay"; see Ralf Brown's interrupt list.  If you
  598.     have DOS 4.0 or later, you can use the MODE CON command that you'll
  599.     find in your DOS manual.
  600.  
  601.     On 83-key keyboards (mostly XTs), the delay and typematic rate can't
  602.     easily be changed.  According to the {PC Magazine} of 15 Jan 1991,
  603.     page 409, to adjust the typematic rate you need "a memory-resident
  604.     program which simply '[watches]' the keyboard to see if you're
  605.     holding down a key ... and after a certain time [starts] stuffing
  606.     extra copies of the held-down key into the buffer."  No source code
  607.     is given in that issue; but I'm told that the QUICKEYS utility that
  608.     {PC} published in 1986 does this sort of watching; you can download
  609.     source and object code in PD1:<MSDOS.PCMAG>VOL5N05.ARC from Simtel.
  610.  
  611. Q307. What is the SysRq key for?
  612.  
  613.     There is no standard use for the key.  The BIOS keyboard routines in
  614.     INT 16 simply ignore it; therefore so do the DOS input routines in
  615.     INT 21 as well as the keyboard routines in libraries supplied with
  616.     high-level languages.
  617.  
  618.     When you press or release a key, the keyboard triggers hardware line
  619.     IRQ1, and the CPU calls INT 9.  INT 9 reads the scan code from the
  620.     keyboard and the shift states from the BIOS data area.
  621.  
  622.     What happens next depends on whether your PC's BIOS supports an
  623.     enhanced keyboard (101 or 102 keys).  If so, INT 9 calls INT 15
  624.     function 4F to translate the scan code.  If the translated scan code
  625.     is 54 hex (for the SysRq key) then INT 9 calls INT 15 function 85
  626.     and doesn't put the keystroke into the keyboard buffer.  The default
  627.     handler of that function does nothing and simply returns.  (If your
  628.     PC has an older BIOS that doesn't support the extended keyboards,
  629.     INT 15 function 4F is not called.  Early ATs have 84-key keyboards,
  630.     so their BIOS calls INT 15 function 85 but nor 4F.)
  631.  
  632.     Thus your program is free to use SysRq for its own purposes, but at
  633.     the cost of some programming.  You could hook INT 9, but it's
  634.     probably easier to hook INT 15 function 85, which is called when
  635.     SysRq is pressed or released.
  636.  
  637. Q308. How can my program tell what kind of keyboard is on the system?
  638.  
  639.     Ralf Brown's Interrupt List includes MEMORY.LST, a detailed
  640.     breakdown by Robin Walker of the contents of the BIOS system block
  641.     that starts at 0040:0000.  Bit 4 of byte 0040:0096 is "1=enhanced
  642.     keyboard installed".  C code to test the keyboard type:
  643.         char far *kbd_stat_byte3 = (char far *)0x00400096UL;
  644.         if (0x10 & *kbd_stat_byte3)
  645.             /* 101- or 102-key keyboard is installed */
  646.  
  647.     {PC Magazine}'s 15 Jan 1991 issue suggests on page 412 that "for
  648.     some clones [the above test] is not foolproof".  If you use this
  649.     method in your program you should provide the user some way to
  650.     override this test, or at least some way to tell your program to
  651.     assume a non-enhanced keyboard.  The {PC Magazine} article suggests
  652.     a different approach to determining the type of keyboard.
  653.  
  654. Q309. How can I tell if input, output, or stderr has been redirected?
  655.  
  656.     Normally, input and output are associated with the console (i.e.,
  657.     with the keyboard and the screen, respectively).  If either is not,
  658.     you know that it has been redirected.  Some source code to check
  659.     this is available at the usual archive sites.
  660.  
  661.     If you program in Turbo Pascal, download the /pc/ts/tspa*.zip
  662.     collection of Turbo Pascal units from garbo; or from Simtel,
  663.     PD1:<MSDOS.TURBOPAS>TSPA*.ZIP.  (Choose TSPA3060.ZIP, TSPA3055.ZIP,
  664.     TSPA3050.ZIP, or TSPA3040.ZIP for Turbo Pascal 6.0, 5.5, 5.0, or 4.0
  665.     respectively.)  Source code is not included.  Also see the
  666.     information in garbo.uwasa.fi:/pc/ts/tsfaq*.zip Frequently Asked
  667.     Questions, the Turbo Pascal section.
  668.  
  669.     If you program in C, use isatty( ) if your implementation has it.
  670.     Otherwise, you can download PD1:<MSDOS.SYSUTL>IS_CON10.ZIP from
  671.     Simtel; it includes source code.
  672.  
  673.     Good references for the principles are {PC Magazine} 16 Apr 1991
  674.     (vol 10 nr 7) pg 374; Ray Duncan's {Advanced MS-DOS}, ISBN
  675.     1-55615-157-8, or Ralf Brown's interrupt list for INT 21 function
  676.     4400; and Terry Dettman and Jim Kyle's {DOS Programmer's Reference:
  677.     2d edition}, ISBN 0-88022-458-4, pp 602-603.
  678.  
  679.  
  680. If the posting date is more than six weeks in the past, see instructions
  681. in part 4 of this list for how to get an updated copy.
  682.  
  683.             Copyright (C) 1992  Stan Brown, Oak Road Systems
  684.  
  685.  
  686. section 4.  Disks and files
  687. ===========================
  688.  
  689. Q401. What drive was the PC booted from?
  690.  
  691.     Under DOS 4.0 or later, load 3305 hex into AX; do an INT 21.  DL is
  692.     returned with an integer indicating the boot drive (1=A:, etc.).
  693.  
  694. Q402. How can I boot from drive b:?
  695.  
  696.     Download PD1:<MSDOS.DSKUTL>BOOT_B.ZIP (shareware) from Simtel.  The
  697.     included documentation says it works by writing a new boot sector on
  698.     a disk in your a: drive that redirects the boot to your b: drive.
  699.  
  700. Q403. Which real and virtual disk drives are valid?
  701.  
  702.     Use INT 21 function 29 (parse filename).  Point DS:SI at a null-
  703.     terminated ASCII string that contains the drive letter and a colon,
  704.     point ES:DI at a 37-byte dummy FCB buffer, set AX to 2900h, and do
  705.     an INT 21.  On return, AL is FF if the drive is invalid, something
  706.     else if the drive is valid.  RAM disks and SUBSTed drives are
  707.     considered valid.
  708.  
  709.     Unfortunately, the b: drive is considered valid even on a single-
  710.     diskette system.  You can check that special case by interrogating
  711.     the BIOS equipment byte at 0040:0010.  Bits 7-6 contain the one less
  712.     than the number of diskette drives, so if those bits are zero you
  713.     know that b: is an invalid drive even though function 29 says it's
  714.     valid.
  715.  
  716.     Following is some code originally posted by Doug Dougherty, with my
  717.     fix for the b: special case, tested only in Borland C++ 2.0 (in
  718.     the small model):
  719.  
  720.         #include <dos.h>
  721.         void drvlist(void)  {
  722.             char *s = "A:", fcb_buff[37];
  723.             int valid;
  724.             for (   ;  *s<='Z';  (*s)++) {
  725.                 _SI = (unsigned) s;
  726.                 _DI = (unsigned) fcb_buff;
  727.                 _ES = _DS;
  728.                 _AX = 0x2900;
  729.                 geninterrupt(0x21);
  730.                 valid = _AL != 0xFF;
  731.                 if (*s == 'B'  &&  valid) {
  732.                     char far *equipbyte = (char far *)0x00400010UL;
  733.                     valid = (*equipbyte & (3 << 6)) != 0;
  734.                 }
  735.                 printf("Drive '%s' is %sa valid drive.\n",
  736.                         s, valid ? "" : "not ");
  737.             }
  738.         }
  739.  
  740. Q404. How can I make my single floppy drive both a: and b:?
  741.  
  742.     Under any DOS since DOS 2.0, you can put the command
  743.  
  744.         assign b=a
  745.  
  746.     into your AUTOEXEC.BAT file.  Then, when you type "DIR B:" you'll no
  747.     longer get the annoying prompt to insert diskette B (and the even
  748.     more annoying prompt to insert A the next time you type "DIR A:").
  749.  
  750.     You may be wondering why anybody would want to do this.  Suppose you
  751.     use two different machines, maybe one at home and one at work.  One
  752.     of them has only a 3.5" diskette drive; the other machine has two
  753.     drives, and b: is the 3.5" one.  You're bound to type "dir b:" on
  754.     the first one, and get the nuisance message
  755.  
  756.         Insert diskette for drive B: and press any key when ready.
  757.  
  758.     But if you assign drive b: to point to a:, you avoid this problem.
  759.  
  760.     Caution:  there are a few commands, such as DISKCOPY, that will not
  761.     work right on ASSIGNed or SUBSTed drives.  See the DOS manual for
  762.     the full list.  Before typing one of those commands, be sure to turn
  763.     off the mapping by typing "assign" without arguments.
  764.  
  765.     The DOS 5.0 manual says that ASSIGN is obsolete, and recommends the
  766.     equivalent form of SUBST: "subst b: a:\".  Unfortunately, if this
  767.     command is executed when a: doesn't hold a diskette, the command
  768.     fails.  ASSIGN doesn't have this problem, so I must advise you to
  769.     disregard that particular bit of advice in the DOS manual.
  770.  
  771. Q405. Why won't my C program open a file with a path?
  772.  
  773.     You've probably got something like the following code:
  774.  
  775.         char *filename = "c:\foo\bar\mumble.dat";
  776.         . . .  fopen(filename, "r");
  777.  
  778.     The problem is that \f is a form feed, \b is a backspace, and \m is
  779.     m.  Whenever you want a backslash in a string constant in C, you
  780.     must use two backslashes:
  781.  
  782.         char *filename = "c:\\foo\\bar\\mumble.dat";
  783.  
  784.     This is a feature of every C compiler, because Dennis Ritchie
  785.     designed C this way.  It's a problem only on MS-DOS systems, because
  786.     only DOS (and Atari ST/TT running TOS, I'm told) uses the backslash
  787.     in directory paths.  But even in DOS this backslash convention
  788.     applies _only_ to string constants in your source code.  For file
  789.     and keyboard input at run time, \ is just a normal character, so
  790.     users of your program would type in file specs at run time the same
  791.     way as in DOS commands, with single backslashes.
  792.  
  793.     Another possibility is to code all paths in source programs with /
  794.     rather than \ characters:
  795.  
  796.         char *filename = "c:/foo/bar/mumble.dat";
  797.  
  798.     Ralf Brown writes that "All versions of the DOS kernel accept either
  799.     forward or backslashes as directory separators.  I tend to use this
  800.     form more frequently than backslashes since it is easier to type and
  801.     read."  This applies to DOS function calls (and therefore to calls
  802.     to the file library of every programming language), but not to DOS
  803.     commands.
  804.  
  805. Q406. How can I redirect printer output to a file?
  806.  
  807.     My personal favorite utility for this purpose is PRN2FILE from {PC
  808.     Magazine}, available from Simtel as PD1:<MSDOS.PRINTER>PRN2FILE.ARC,
  809.     or from garbo as prn2file.zip in /pc/printer.  ({PC Magazine} has
  810.     given copies away as part of its utilities disks, so you may already
  811.     have a copy.)
  812.  
  813.     Check the PD1:<MSDOS.PRINTER> directory at Simtel, or /pc/printer
  814.     at garbo, for lots of other printer-redirection utilities.
  815.  
  816. Q407. How can my program open more files than DOS's limit of 20?
  817.  
  818.     (This is a summary of an article Ralf Brown posted on 8 August 1992.)
  819.  
  820.     There are separate limits on files and file handles.  For example,
  821.     DOS opens three files but five file handles:  CON (stdin, stdout,
  822.     and stderr), AUX (stdaux), and PRN (stdprn).
  823.  
  824.     The limit in FILES= in CONFIG.SYS is a system-wide limit on files
  825.     opened by all programs (including the three that DOS opens and any
  826.     opened by TSRs); each process has a limit of 20 handles (including
  827.     the five that DOS opens).  Example:  CONFIG.SYS has FILES=40.  Then
  828.     program #1 will be able to open 15 file handles.  Assuming that the
  829.     program actually does open 15 handles pointing to 15 different
  830.     files, other programs could still open a total of 22 files (40-3-15
  831.     = 22), though no one program could open more than 15 file handles.
  832.  
  833.     If you're running DOS 3.3 or later, you can increase the per-process
  834.     limit of 20 file handles by a call to INT 21 function 67, Set Handle
  835.     Count.  Your program is still limited by the system-wide limit on
  836.     open files, so you may also need to increase the FILES= value in
  837.     your CONFIG.SYS file (and reboot).  The run-time library that you're
  838.     using may have a fixed-size table of file handles, so you may also
  839.     need to get source code for the module that contains the table,
  840.     increase the table size, and recompile it.
  841.  
  842. Q408. How can I read, create, change, or delete the volume label?
  843.  
  844.     In DOS 5.0 (and, I believe, in 4.0 as well), there are actually two
  845.     volume labels: one, the traditional one, is an entry in the root
  846.     directory of the disk; and the other is in the boot record along
  847.     with the serial number (see next Q).  The DIR and VOL commands
  848.     report the traditional label; the LABEL command reports the
  849.     traditional one but changes both of them.
  850.  
  851.     In DOS 4.0 and later, use INT 21 function 69 to access the boot
  852.     record's serial number and volume label together; see the next Q.
  853.  
  854.     Assume that by "volume label" you mean the traditional one, the one
  855.     that DIR and VOL display.  Though it's a directory entry in the root
  856.     directory, you can't change it using the newer DOS file-access
  857.     functions (3C, 41, 43); instead, use the old FCB-oriented directory
  858.     functions.  Specifically, you need to allocate a 64-byte buffer and
  859.     a 41- byte extended FCB (file control block).  Call INT 21 AH=1A to
  860.     find out whether there is a volume label.  If there is, AL returns 0
  861.     and you can change the label using DOS function 17 or delete it
  862.     using DOS function 13.  If there's no volume label, function 1A will
  863.     return FF and you can create a label via function 16.  Important
  864.     points to notice are that ? wildcards are allowed but * are not; the
  865.     volume label must be space filled not null terminated.
  866.  
  867.     The following MSC 7.0 code worked for me in DOS 5.0; the functions
  868.     it uses have been around since DOS 2.0.  The function parameter is 0
  869.     for the current disk, 1 for a:, 2 for b:, etc.  It doesn't matter
  870.     what your current directory is; these functions always search the
  871.     root directory for volume labels.  (I didn't try to change the
  872.     volume label of any networked drives.)
  873.  
  874.     // Requires DOS.H, STDIO.H, STRING.H
  875.     void vollabel(unsigned char drivenum) {
  876.         static unsigned char extfcb[41], dta[64], status, *newlabel;
  877.         int chars_got = 0;
  878.         #define DOS(buff,func) __asm { __asm mov dx,offset buff \
  879.             __asm mov ax,seg buff  __asm push ds  __asm mov ds,ax \
  880.             __asm mov ah,func  __asm int 21h  __asm pop ds \
  881.             __asm mov status,al }
  882.         #define getlabel(buff,prompt) newlabel = buff;  \
  883.             memset(newlabel,' ',11);  printf(prompt);   \
  884.             scanf("%11[^\n]%n", newlabel, &chars_got);  \
  885.             if (chars_got < 11) newlabel[chars_got] = ' ';
  886.  
  887.         // Set up the 64-byte transfer area used by function 1A.
  888.         DOS(dta, 1Ah)
  889.         // Set up an extended FCB and search for the volume label.
  890.         memset(extfcb, 0, sizeof extfcb);
  891.         extfcb[0] = 0xFF;             // denotes extended FCB
  892.         extfcb[6] = 8;                // volume-label attribute bit
  893.         extfcb[7] = drivenum;         // 1=A, 2=B, etc.; 0=current drive
  894.         memset(&extfcb[8], '?', 11);  // wildcard *.*
  895.         DOS(extfcb,11h)
  896.         if (status == 0) {            // DTA contains volume label's FCB
  897.             printf("volume label is %11.11s\n", &dta[8]);
  898.             getlabel(&dta[0x18], "new label (\"delete\" to delete): ");
  899.             if (chars_got == 0)
  900.                 printf("label not changed\n");
  901.             else if (strncmp(newlabel,"delete     ",11) == 0) {
  902.                 DOS(dta,13h)
  903.                 printf(status ? "label failed\n" : "label deleted\n");
  904.             }
  905.             else {                    // user wants to change label
  906.                 DOS(dta,17h)
  907.                 printf(status ? "label failed\n" : "label changed\n");
  908.             }
  909.         }
  910.         else {                        // no volume label was found
  911.             printf("disk has no volume label.\n");
  912.             getlabel(&extfcb[8], "new label (<Enter> for none): ");
  913.             if (chars_got > 0) {
  914.                 DOS(extfcb,16h)
  915.                 printf(status ? "label failed\n" : "label created\n");
  916.             }
  917.         }
  918.     }   // end function vollabel
  919.  
  920. Q409. How can I get the disk serial number?
  921.  
  922.     Use INT 21.  AX=6900 gets the serial number; AX=6901 sets it.  See
  923.     Ralf Brown's interrupt list, or page 496 of the July 1992 {PC
  924.     Magazine}, for details.
  925.  
  926.     This function also gets and sets the volume label, but it's the
  927.     volume label in the boot record, not the volume label that a DIR
  928.     command displays.  See the preceding Q.
  929.  
  930. Q410. What's the format of .OBJ, .EXE., .COM files?
  931.  
  932.     Please see section 2, "Compile and link".
  933.  
  934.  
  935. section 5. Serial ports (COM ports)
  936. ===================================
  937.  
  938. Q501. How do I set my machine up to use COM3 and COM4?
  939.  
  940.     Unless your machine is fairly old, it's probably already set up.
  941.     After installing the board that contains the extra COM port(s),
  942.     check the I/O addresses in word 0040:0004 or 0040:0006.  (In DEBUG,
  943.     type "D 40:4 L4" and remember that every word is displayed low
  944.     byte first, so if you see "03 56" the word is 5603.)  If those
  945.     addresses are nonzero, your PC is ready to use the ports and you
  946.     don't need the rest of this answer.
  947.  
  948.     If the I/O address words in the 0040 segment are zero after you've
  949.     installed the I/O board, you need some code to store these values
  950.     into the BIOS data segment:
  951.  
  952.         0040:0004  word  I/O address of COM3
  953.         0040:0006  word  I/O address of COM4
  954.         0040:0011  byte (bits 3-1): number of serial ports installed
  955.  
  956.     The documentation with your I/O board should tell you the port
  957.     addresses.  When you know the proper port addresses, you can add
  958.     code to your program to store them and the number of serial ports
  959.     into the BIOS data area before you open communications.  Or you can
  960.     use DEBUG to create a little program to include in your AUTOEXEC.BAT
  961.     file, using this script:
  962.  
  963.             n SET_ADDR.COM      <--- or a different name ending in .COM
  964.             a 100
  965.             mov  AX,0040
  966.             mov  DS,AX
  967.             mov  wo [0004],aaaa <--- replace aaaa with COM3 address or 0
  968.             mov  wo [0006],ffff <--- replace ffff with COM4 address or 0
  969.             and  by [0011],f1
  970.             or   by [0011],8    <--- use number of serial ports times 2
  971.             mov  AH,0
  972.             int  21
  973.                                 <--- this line must be blank
  974.             rCX
  975.             1f
  976.             rBX
  977.             0
  978.             w
  979.             q
  980.  
  981. Q502. How do I find the I/O address of a COM port?
  982.  
  983.     Look in the four words beginning at 0040:0000 for COM1 through COM4.
  984.     (The DEBUG command "D 40:0 L8" will do this.  Remember that words
  985.     are stored and displayed low byte first, so a word value of 03F8
  986.     will be displayed as F8 03.)  If the value is zero, that COM port is
  987.     not installed (or you've got an old BIOS; see the preceding Q).  If
  988.     the value is nonzero, it is the I/O address of the transmit/receive
  989.     register for the COM port.  Each COM port occupies eight consecutive
  990.     I/O addresses (though only seven are used by many chips).
  991.  
  992.     Here's some C code to find the I/O address:
  993.  
  994.         unsigned ptSel(unsigned comport) {
  995.             unsigned io_addr;
  996.             if (comport >= 1  &&  comport <= 4) {
  997.                 unsigned far *com_addr = (unsigned far *)0x00400000UL;
  998.                 io_addr = com_addr[comport-1];
  999.             }
  1000.             else
  1001.                 io_addr = 0;
  1002.             return io_addr;
  1003.         }
  1004.  
  1005. Q503. But aren't the COM ports always at I/O addresses 3F8, 2F8, 3E8,
  1006.       and 2E8?
  1007.  
  1008.     The first two are usually right (though not always); the last two
  1009.     are different on many machines.
  1010.  
  1011. Q504. How do I configure a COM port and use it to transmit data?
  1012.  
  1013.     After hearing several recommendations, I looked at Joe Campbell's {C
  1014.     Programmer's Guide to Serial Communications}, ISBN 0-672-22584-0,
  1015.     and agree that it is excellent.  He gives complete details on how
  1016.     serial ports work, along with complete programs for doing polled or
  1017.     interrupt-driver I/O.  The book is quite thick, and none of it looks
  1018.     like filler.
  1019.  
  1020.     If Campbell's book is overkill for you, you'll find a good short
  1021.     description of serial I/O in {DOS 5: A Developer's Guide}, ISBN
  1022.     1-55851-177-6, by Al Williams.
  1023.  
  1024.     You may also want to look at an extended example in Borland's
  1025.     TechFax TI445, part of PD1:<MSDOS.TURBO-C> at Simtel.  Though
  1026.     written by Borland, much of it is applicable to other forms of C,
  1027.     and it should give you ideas for other programming languages.
  1028.  
  1029. section 6. Other hardware questions and problems
  1030. ================================================
  1031.  
  1032. Q601. Which 80x86 CPU is running my program?
  1033.  
  1034.     According to an article posted by Michael Davidson, Intel's approved
  1035.     code for distinguishing among 8086, 80286, 80386, and 80486 and for
  1036.     detecting the presence of an 80287 or 80387 is published in the
  1037.     Intel's 486SX processor manual (order number 240950-001).  You can
  1038.     download David Kirschbaum's improved version of this from Simtel as
  1039.     PD1:<MSDOS.SYSUTL>CPUID593.ZIP.
  1040.  
  1041.     According to an article posted by its author, WCPU041.ZIP knows the
  1042.     differences between DX and SX varieties of 386 and 486 chips, and
  1043.     can also detect a math coprocessor.  It's in PD1:<MSDOS.SYSUTL> at
  1044.     Simtel.
  1045.  
  1046. Q602. How can a C program send control codes to my printer?
  1047.  
  1048.     If you just fprintf(stdprn, ...), C will translate some of your
  1049.     control codes.  The way around this is to reopen the printer in
  1050.     binary mode:
  1051.  
  1052.         prn = fopen("PRN", "wb");
  1053.  
  1054.     You must use a different file handle because stdprn isn't an lvalue.
  1055.     By the way, PRN or LPT1 must not be followed by a colon in DOS 5.0.
  1056.  
  1057.     There's one special case, Ctrl-Z (ASCII 26), the DOS end-of-file
  1058.     character.  If you try to send an ASCII 26 to your printer, DOS
  1059.     simply ignores it.  To get around this, you need to reset the
  1060.     printer from "cooked" to "raw" mode.  Microsoft C users must use int
  1061.     21 function 44, "get/set device information".  Turbo C and Borland
  1062.     C++ users can use ioctl to accomplish the same thing:
  1063.  
  1064.         ioctl(fileno(prn), 1, ioctl(fileno(prn),0) & 0xFF | 0x20, 0);
  1065.  
  1066.     An alternative approach is simply to write the printer output into a
  1067.     disk file, then copy the file to the printer with the /B switch.
  1068.  
  1069.     A third approach is to bypass DOS functions entirely and use the
  1070.     BIOS printer functions at INT 17.  If you also fprintf(stdprn,...)
  1071.     in the same program, you'll need to use fflush( ) to synchronize
  1072.     fprintf( )'s buffered output with the BIOS's unbuffered.
  1073.  
  1074.     By the way, if you've opened the printer in binary mode from a C
  1075.     program, remember that outgoing \n won't be translated to carriage
  1076.     return/line feed.  Depending on your printer, you may need to send
  1077.     explicit \n\r sequences.
  1078.  
  1079. Q603. How can I redirect printer output to a file?
  1080.  
  1081.     Please see section 4, "Disks and files", for the answer.
  1082.  
  1083. Q604. Which video adapter is installed?
  1084.  
  1085.     The technique below should work if your BIOS is not too old.  It
  1086.     uses three functions from INT 10, the BIOS video interrupt.  (If
  1087.     you're using a Borland language, you may not have to do this the
  1088.     hard way.  Look for a function called DetectGraph or something
  1089.     similar.)
  1090.  
  1091.     Set AH=12h, AL=0, BL=32h; INT 10h.  If AL is 12h, you have a VGA.
  1092.     If not, set AH=12h, BL=10h; INT 10h.  If BL is 0,1,2,3, you have an
  1093.     EGA with 64,128,192,256K memory.  If not, set AH=0Fh; INT 10h.  If
  1094.     AL is 7, you have an MDA (original monochrome adapter) or Hercules;
  1095.     if not, you have a CGA.
  1096.  
  1097.     I've tested this for my VGA and got the right answer; but I can't
  1098.     test it for the other equipment types.  Please let me know by email
  1099.     at brown@ncoast.org if your results vary.
  1100.  
  1101. Q605. How do I switch to 43- or 50-line mode?
  1102.  
  1103.     Download PD1:<MSDOS.SCREEN>VIDMODE.ZIP from Simtel or one of the
  1104.     mirror sites.  It contains .COM utilities and .ASM source code.
  1105.  
  1106. Q606. How can I find the Microsoft mouse position and button status?
  1107.  
  1108.     Use INT 33 function 3, described in Ralf Brown's interrupt list.
  1109.  
  1110.     The Windows manual says that the Logitech mouse is compatible with
  1111.     the Microsoft one, so I assume the interrupt will work the same.
  1112.  
  1113.     Also, see the directory PD1:<MSDOS.MOUSE> at Simtel.
  1114.  
  1115. Q607. How can I access a specific address in the PC's memory?
  1116.  
  1117.     First check the library that came with your compiler.  Many vendors
  1118.     have some variant of peek and poke functions; in Turbo Pascal use
  1119.     the pseudo-arrays Mem, MemW, and MemL.  As an alternative, you can
  1120.     construct a far pointer:  use Ptr in Turbo Pascal, MK_FP in the
  1121.     Turbo C family, and FP_OFF and FP_SEG in Microsoft C.
  1122.  
  1123.     Caution:  Turbo C and Turbo C++ also have FP_OFF and FP_SEG macros,
  1124.     but they can't be used to construct a pointer.  In Borland C++ those
  1125.     macros work the same as in Microsoft C, but MK_FP is easier to use.
  1126.  
  1127.     By the way, it's not useful to talk about "portable" ways to do
  1128.     this.  Any operation that is tied to a specific memory address is
  1129.     not likely to work on another kind of machine.
  1130.  
  1131. Q608. How can I read or write my PC's CMOS memory?
  1132.  
  1133.     There are a great many public-domain utilities that do this.  These
  1134.     were available for download from Simtel as of 31 March 1992:
  1135.  
  1136.     PD1:<MSDOS.AT>
  1137.     CMOS14.ZIP     5965  920817  Saves/restores CMOS to/from file
  1138.     CMOSER11.ZIP  28323  910721  386/286 enhanced CMOS setup program
  1139.     CMOSRAM.ZIP   76096  920214  Save AT/386/486 CMOS data to file and restore
  1140.     ROM2.ARC      20497  900131  Save AT and 386 CMOS data to file and restore
  1141.     SETUP21.ARC   24888  880613  Setup program which modifies CMOS RAM
  1142.     VIEWCMOS.ARC  15374  900225  Display contents of AT CMOS RAM, w/C source
  1143.  
  1144.     At garbo, /pc/ts/tsutle17.zip contains a CMOS program to check and
  1145.     display CMOS memory, but not to write to it.
  1146.  
  1147.     I have heard good reports of CMOS299.ZIP, available in the pc.dir
  1148.     directory of cantva.canterbury.ac.nz [132.181.30.3].
  1149.  
  1150.     Of the above, my only experience is with CMOSRAM, which seems to
  1151.     work fine.  It contains an excellent (and witty) .DOC file that
  1152.     explains the hardware involved and gives specific recommendations
  1153.     for preventing disaster or recovering from it.  It's $5 shareware.
  1154.  
  1155.     Robert Jourdain's {Programmer's Problem Solver for the IBM PC, XT,
  1156.     and AT} has code for accessing the CMOS RAM, according to an article
  1157.     posted in this newsgroup.
  1158.  
  1159. Q609. How can I access memory beyond 640K?
  1160.  
  1161.     I'm outside my expertise on this one, but in late 1992 Jamshid
  1162.     Afshar (jamshid@emx.utexas.edu) kindly supplied the following, which
  1163.     incorporates some corrections agreed with Duncan Murdoch (dmurdoch@
  1164.     mast.queensu.ca).  If you have any corrections or comments, please
  1165.     send them to both the above addresses.
  1166.  
  1167.     ...........................(begin quote)............................
  1168.     1. Use XMS or EMS memory.  XMS is preferable in most cases, but
  1169.     some machines won't provide it.  There are some libraries available
  1170.     at Simtel to access XMS or EMS.  The disadvantage is that you
  1171.     don't allocate the memory as you would with malloc() (or `new' in
  1172.     C++).  I believe it also requires that you lock this memory when in
  1173.     use.  This means your code is not easily ported to other (and
  1174.     future) operating systems and that your code is more convoluted than
  1175.     it would be under a "real" os.  The advantage is that the library
  1176.     works with compilers since Turbo C 2.0 (I think) and that your
  1177.     program will easily run on even 286s.
  1178.  
  1179.     2.  Program under MS Windows.  MS Windows functions as a 16-bit DOS
  1180.     Extender (see #3).  Borland/Turbo C++ 3.x includes EasyWin [and
  1181.     Microsoft C/C++ 7.0 has QuickWin --ed.] which is a library that
  1182.     automatically lets you compile your current code using C/C++
  1183.     standard input or <conio.h> into a MS Windows program so your code
  1184.     can immediately allocate many MBs of memory (Windows enhanced mode
  1185.     even does virtual memory).  The disadvantage is that like any 16-bit
  1186.     Extender a single malloc() is restricted to 64K (unless you want to
  1187.     mess with huge pointers in Windows).  Also, EasyWin's screen output
  1188.     is significantly slower than a DOS character-mode program's and you
  1189.     must of course run the program from Windows.
  1190.  
  1191.     3.  Use a 16-bit or 32-bit DOS Extender.  This is definitely the
  1192.     best solution from the programmer's standpoint.  You just allocate
  1193.     as much memory as you need using malloc() or 'new'.  A 16-bit
  1194.     Extender still has 16-bit ints and restricts arrays to 64K, but a
  1195.     32-bit Extender has 32-bits ints (which makes porting a lot of UNIX
  1196.     code easier) so there are no 64K limits.  A 32-bit Extender requires
  1197.     a 32-bit compiler and the program will not run on 286s.  Some
  1198.     Extenders also do virtual memory.  Using an Extender doesn't require
  1199.     source code changes and unlike option #1 your code is portable and
  1200.     not obsolete in a few months.  Your options for this solution are:
  1201.  
  1202.     - Buy PharLap's 16-bit Extender that works with BC++ 3.0+ and MSC
  1203.       (just requires a relink).  Note, the BC++ 3.1 upgrade came with
  1204.       PharLap "lite".  Pharlap's 32-bit Extender works with 32-bit
  1205.       compilers like [?]
  1206.  
  1207.     - Get the GNU (free,copylefted) gcc 2.x compiler which DJ Delorie
  1208.       ported from UNIX and which uses his 32-bit Extender.  It supports
  1209.       C and C++, but the Extender is VCPI which means neither the
  1210.       compiler nor programs it produces will run in a DOS session under
  1211.       Windows.  FTP to barnacle.erc.clarkson.edu and get
  1212.       pub/msdos/djgpp/readme.
  1213.  
  1214.     - Get a 32-bit compiler or one that comes with a DOS Extender.
  1215.       Zortech comes with 16-bit and a 32-bit Extenders (no debugger for
  1216.       32-bit programs, but Flashtek sells one).  Watcom also makes a C
  1217.       [and C++?] 32-bit compiler.  [If anyone else has products or plans
  1218.       to announce, please let me know.]
  1219.  
  1220.     - Buy Borland Pascal 7.0.  It includes a 16 bit royalty-free DOS
  1221.       extender using the same interface as MS Windows.  It functions
  1222.       under a DPMI server like Windows or QDPMI from Quarterdeck, and
  1223.       also provides its own server which you can distribute with your
  1224.       programs.
  1225.  
  1226.     4.  This option doesn't really count since it's not a solution in
  1227.     DOS, but you could switch to a full 32-bit operating system like
  1228.     OS/2 2.0 or UNIX (or NT when it comes out).  I believe Win32 will
  1229.     allow you to write 32-bit Windows programs.  [can someone fill me in
  1230.     on what exactly Win32 is?]
  1231.     ............................(end quote).............................
  1232.  
  1233.  
  1234. section 7. Other software questions and problems
  1235. ================================================
  1236.  
  1237. Q701. How can a program reboot my PC?
  1238.  
  1239.     You can generate a "cold" boot or a "warm" boot.  A cold boot is
  1240.     the same as turning the power off and on; a warm boot is the same as
  1241.     Ctrl-Alt-Del and skips the power-on self test.
  1242.  
  1243.     For a warm boot, store the hex value 1234 in the word at 0040:0072.
  1244.     For a cold boot, store 0 in that word.  Then, if you want to live
  1245.     dangerously, jump to address FFFF:0000.  Here's C code to do it:
  1246.  
  1247.         /* WARNING:  data loss possible */
  1248.         void bootme(int want_warm)  /* arg 0 = cold boot, 1 = warm */ {
  1249.             void (far* boot)(void) = (void (far*)(void))0xFFFF0000UL;
  1250.             unsigned far* type = (unsigned far*)0x00400072UL;
  1251.             *type = (want_warm ? 0x1234 : 0);
  1252.             (*boot)( );
  1253.         }
  1254.  
  1255.     What's wrong with that method?  It will boot right away, without
  1256.     closing files, flushing disk caches, etc.  If you boot without
  1257.     flushing a write-behind disk cache (if one is running), you could
  1258.     lose data or even trash your hard drive.
  1259.  
  1260.     There are two methods of signaling the cache to flush its buffers:
  1261.     (1) simulate a keyboard Ctrl-Alt-Del in the keystroke translation
  1262.     function of the BIOS (INT 15 function 4F), and (2) issue a disk
  1263.     reset (DOS function 0D).  Most disk-cache programs hook one or both
  1264.     of those interrupts, so if you use both methods you'll probably be
  1265.     safe.
  1266.  
  1267.     When user code simulates a Ctrl-Alt-Del, one or more of the programs
  1268.     that have hooked INT 15 function 4F can ask that the key be ignored by
  1269.     clearing the carry flag.  For example, HyperDisk does this when it
  1270.     has started but not finished a cache flush.  So if the carry flag
  1271.     comes back cleared, the boot code has to wait a couple of cluck
  1272.     ticks and then try again.  (None of this matters on older machines
  1273.     whose BIOS can't support 101- or 102-key keyboards; see "What is the
  1274.     SysRq key for?" in section 3, "Keyboard".)
  1275.  
  1276.     Here's C code that tries to signal the disk cache (if any) to flush:
  1277.  
  1278.         #include <dos.h>
  1279.         void bootme(int want_warm)  /* arg 0 = cold boot, 1 = warm */ {
  1280.             union REGS reg;
  1281.             void    (far* boot)(void) = (void (far*)(void))0xFFFF0000UL;
  1282.             unsigned far* boottype    =     (unsigned far*)0x00400072UL;
  1283.             char     far* shiftstate  =         (char far*)0x00400017UL;
  1284.             unsigned      ticks;
  1285.             int           time_to_waste;
  1286.             /* Simulate reception of Ctrl-Alt-Del: */
  1287.             for (;;) {
  1288.                 *shiftstate |= 0x0C;    /* turn on Ctrl & Alt */
  1289.                 reg.x.ax = 0x4F53;      /* 0x53 = Del's scan code */
  1290.                 reg.x.cflag = 1;        /* sentinel for ignoring key */
  1291.                 int86(0x15, ®, ®);
  1292.                 /* If carry flag is still set, we've finished. */
  1293.                 if (reg.x.cflag)
  1294.                     break;
  1295.                 /* Else waste some time before trying again: */
  1296.                 reg.h.ah = 0;
  1297.                 int86(0x1A, ®, ®);/* system time into CX:DX */
  1298.                 ticks = reg.x.dx;
  1299.                 for (time_to_waste = 3;  time_to_waste > 0;  ) {
  1300.                     reg.h.ah = 0;
  1301.                     int86(0x1A, ®, ®);
  1302.                     if (ticks != reg.x.dx)
  1303.                         ticks = reg.x.dx , --time_to_waste;
  1304.                 }
  1305.             }
  1306.             /* Issue a DOS disk reset request: */
  1307.             reg.h.ah = 0x0D;
  1308.             int86(0x21, ®, ®);
  1309.             /* Set boot type and boot: */
  1310.             *boottype = (want_warm ? 0x1234 : 0);
  1311.             (*boot)( );
  1312.         }
  1313.  
  1314. Q702. How can I time events with finer resolution than the system
  1315.       clock's 55 ms (about 18 ticks a second)?
  1316.  
  1317.     The following files, among others, can be downloaded from Simtel:
  1318.  
  1319.     PD1:<MSDOS.AT>
  1320.     ATIM.ARC       5946  881126  Precision program timing for AT
  1321.  
  1322.     PD1:<MSDOS.C>
  1323.     MILLISEC.ZIP  37734  911205  MSC/asm src for millisecond res timing
  1324.     MSCHRT3.ZIP   53708  910605  High-res timer toolbox for MSC 5.1
  1325.     MSEC_12.ZIP    8484  920320  High-def millisec timer v1.2 (C,ASM)
  1326.     ZTIMER11.ZIP  77625  920428  Microsecond timer for C, C++, ASM
  1327.  
  1328.     PD1:<MSDOS.TURBO-C>
  1329.     TCHRT3.ZIP    53436  910606  High-res timer toolbox for Turbo C 2.0
  1330.     TCTIMER.ARC   20087  891030  High-res timing of events for Turbo C
  1331.  
  1332.     PD1:<MSDOS.TURBOPAS>
  1333.     BONUS507.ARC 150435  900205  [Turbo Pascal source: high-res timing]
  1334.  
  1335.     Pascal users can download source code in /pc/turbopas/bonus507.zip
  1336.     at garbo.
  1337.  
  1338. Q703. How can I find the error level of the previous program?
  1339.  
  1340.     First, which previou